package behavioral.chain;

/**
 * @author fay
 */
public abstract class AbstractHandler {
	private Integer type;
	private AbstractHandler nextHandler;

	public void setType(Integer type) {
		this.type = type;
	}

	public Integer getType() {
		return type;
	}

	protected void setNextHandler(AbstractHandler nextHandler) {
		this.nextHandler = nextHandler;
	}

	protected void doAction(Integer type) {
		if (this.isAction(type)) {
			this.action();
			if (this.isChain()) {
				this.nextHandler.doAction(type);
			}
		} else {
			if (this.nextHandler != null) {
				this.nextHandler.doAction(type);
			} else {
				System.out.println("未找到处理器");
			}
		}
	}

	/** 具体处理 */
	protected abstract void action();

	/**
	 * 是否处理
	 * @param type 类型
	 * @return 是否
	 */
	protected abstract boolean isAction(Integer type);

	/**
	 * 处理完成后是否需要其他责任链继续处理
	 * @return 是否
	 */
	protected boolean isChain() {
		return false;
	}
}
